package edu.jmu.action.privilege;


import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import edu.jmu.bean.privilege.Privilege;
import edu.jmu.utils.GenericsUtils;


@Aspect	@Component
public class Interceptor {
	/**
	 * 定义切面
	 */
	@SuppressWarnings("unused")
	@Pointcut(" execution(java.lang.String edu.jmu.action..*.*()) && " + 
			  "!execution(java.lang.String edu.jmu.action..*.set*()) && " + 
			  "!execution(java.lang.String edu.jmu.action..*.get*())")
	private void intercepterMethod(){}
	
	@Around("intercepterMethod()")
	public Object doActionClassProfilling(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {	
		Object returnValue = null;
		Privilege privilege = GenericsUtils.getMethodPermission(proceedingJoinPoint.getTarget().getClass(),proceedingJoinPoint.getSignature().getName());
		
		
		//没有获得注解  及不需要权限-- 则直接运行
		if( privilege == null || "".equals(privilege)){
			System.out.println("not annotation:" + proceedingJoinPoint.getSignature().getName());
			returnValue = proceedingJoinPoint.proceed();
		}else{
			ServletActionContext.getResponse().sendRedirect("/oa/error.jsp");
			System.out.println(privilege.getModule() + "  " + privilege.getPrivilege());
			returnValue = proceedingJoinPoint.proceed();
		}
		
		return returnValue;
	}
}
